<template>
    <div class="app-container tz-app">


        <div class="zt-select flex-s">
            <div class="flex-left flex">
                <!-- <el-select v-model="queryParams.radioStationName" filterable placeholder="电台名称" class="zt-input rihgt-m5">
                    <el-option v-for="item in options" :key="item.label" :label="item.label" :value="item.label">
                    </el-option>
                </el-select> -->
                <el-input class="zt-input rihgt-m5" v-model="queryParams.radioStationName" placeholder="电台名称" />
                <button class="bt-blue " @click="handleQuery">筛选</button>

            </div>
            <!-- <button class="bt-yellow " @click="handleAdd">新增</button> -->
            <!-- <el-input v-model="queryParams.antennaName" placeholder="请输入天线名称" class="zt-input" clearable
                    @keyup.enter.native="handleQuery" /> -->

        </div>

        <div class="tz-table-box" style="height:calc(100vh - 250px);">
            <el-table v-loading="loading" :data="stationList" @selection-change="handleSelectionChange"
                height="calc(100vh - 250px)" class="zt-table" :empty-text="'暂无数据'">
                <!-- <el-table-column show-overflow-tooltip label="电台ID" align="center" prop="radioStationId" /> -->
                <el-table-column type="index" width="50"> </el-table-column>
                <el-table-column show-overflow-tooltip label="电台编号" align="center" prop="radioStationCode" />
                <el-table-column show-overflow-tooltip label="电台名称" align="center" prop="radioStationName"
                    width="150" />
                <el-table-column show-overflow-tooltip label="电台类型" align="center" prop="radioStationType">
                    <template slot-scope="scope">
                        {{ getRadioStationType(scope.row.radioStationType) }}
                    </template>
                </el-table-column>
                <el-table-column show-overflow-tooltip label="软件版本号" align="center" prop="softwareVersion"
                    width="110" />
                <el-table-column show-overflow-tooltip label="硬件版本号" align="center" prop="hardwareVersion"
                    width="110" />
                <el-table-column show-overflow-tooltip label="电台GPS" align="center" prop="radioStationGps"
                    width="210" />
                <!-- <el-table-column show-overflow-tooltip label="搭载类型" align="center" prop="carryType" /> -->
                <!-- <el-table-column show-overflow-tooltip label="所属单位" align="center" prop="unit" /> -->
                <!-- <el-table-column show-overflow-tooltip label="地址" align="center" prop="address" /> -->
                <el-table-column show-overflow-tooltip label="入网状态" align="center" prop="isNet">
                    <template slot-scope="scope">
                        <div v-if="scope.row.isNet == 0" class="red">未登记</div>
                        <div v-if="scope.row.isNet != 0">已登记</div>
                    </template>
                </el-table-column>
                <el-table-column show-overflow-tooltip label="状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status == 0" class="red">在线</div>
                        <div v-else-if="scope.row.status == 2">离线</div>
                        <div v-else>异常</div>
                    </template>
                </el-table-column>
                <el-table-column show-overflow-tooltip label="sdr状态" align="center" prop="sdrStatus">
                    <template slot-scope="scope">
                        <div v-if="scope.row.sdrStatus == 0" class="red">在线</div>
                        <div v-else>离线</div>
                    </template>
                </el-table-column>
                <!-- <el-table-column show-overflow-tooltip label="备注" align="center" prop="remark" /> -->
                <el-table-column show-overflow-tooltip label="操作" align="center" class-name="small-padding fixed-width"
                    width="150">
                    <template slot-scope="scope">
                        <img src="@/assets/images/amend.png" class="icon-img" @click="handleUpdate(scope.row)"
                            title="编辑" />
                        <img src="@/assets/images/delete-bin2.png" class="icon-img" @click="handleDelete(scope.row)"
                            title="删除" />
                        <img src="@/assets/images/xs.png" @click="handleDetails(scope.row)" class="icon-img"
                            title="详情" />
                        <img src="@/assets/images/st.png" @click="postRestartRadioStation(scope.row)"
                            style="width: 20px;height: 20px;margin-top: 10px; cursor: pointer;" title="重启" />
                        <!-- <el-popover placement="top-start" popper-class="zt-popper" width="100" trigger="click"
                            @after-enter="restartOpen(scope.row)" @hide="restartClose">
                            <template #reference>
                                <img src="@/assets/images/st.png"
                                    style="width: 20px;height: 20px;margin-top: 10px; cursor: pointer;">
                            </template>
    <template v-show="popoverOpen" #default>
                                <div class="restartMenu">
                                    <div class="restart-li">重启服务</div>
                                    <div class="restart-li">关闭电源</div>
                                </div>
                            </template>
    </el-popover> -->


                    </template>
                </el-table-column>
            </el-table>
        </div>



        <div class="zt-pagination">
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>




        <right-dialog :title="title" :visible="open" @closeDialog="cancel">
            <template v-slot:default>
                <div class="zt-dialog-solt">
                    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                        <!-- v-if="form.radioStationType == 1001" -->
                        <el-form-item label="类型:">
                            <div class="flex">
                                <el-select v-model="form.radioStationType" filterable placeholder="类型"
                                    @change="changeType" class="zt-input-min rihgt-m5"
                                    style="width: 120px;font-weight: 600;">
                                    <el-option v-for="item in  dict.type.radio_station_type" :key="item.value"
                                        :label="item.label" :value="item.value" />
                                </el-select>
                                <el-form-item label="所属地:" prop="locatedAddress">
                                    <el-select v-model="form.locatedAddress" filterable placeholder="所属地"
                                        @change="changeLocatedAddress" class="zt-input-min rihgt-m5"
                                        style="width: 120px;font-weight: 600;font-size: 15px;">
                                        <el-option v-for="item in  dict.type.located_address" :key="item.value"
                                            :label="item.label" :value="item.value" />
                                    </el-select>
                                </el-form-item>

                                <el-form-item label="天线:" v-show="form.locatedAddress">
                                    <el-select v-model="form.antennaId" :popper-append-to-body="false" placeholder="请选择"
                                        :popper-class="'t-select'" class="zt-input-min rihgt-m5" filterable
                                        style="width: 90%;">
                                        <el-option-group v-for="group in antennaListAll" :key="group.label"
                                            :label="group.label">
                                            <el-option v-for="item in group.options" :key="item.antennaId"
                                                :label="item.antennaName" :value="item.antennaId">
                                            </el-option>
                                        </el-option-group>
                                    </el-select>
                                </el-form-item>


                            </div>
                        </el-form-item>

                        <el-form-item label="硬件版本号:">
                            <div class="flex">
                                <el-input class="zt-input-min" style="width: 120px;" v-model="form.hardwareVersion"
                                    placeholder="版本号" />


                                <el-form-item label="软件版本号:">
                                    <el-input class="zt-input-min" style="width: 120px;" v-model="form.softwareVersion"
                                        placeholder="版本号" />
                                </el-form-item>


                                <el-form-item label="入网状态::">
                                    <el-select v-model="form.isNet" filterable placeholder="入网状态" :disabled="isNet == 1"
                                        class="zt-input-min rihgt-m5">
                                        <el-option key="0" label="未入网" value="0" />
                                        <el-option key="1" label="已入网" value="1" />
                                    </el-select>
                                </el-form-item>

                            </div>

                        </el-form-item>

                        <div class="flex">
                            <el-form-item label="供应商:">
                                <el-input class="zt-input-min" v-model="form.supplier" placeholder="请输入供应商" />
                            </el-form-item>
                            <el-form-item label="出厂日期:">
                                <el-date-picker class="zt-input-min" style="width: 100%;" value-format="yyyy-MM-dd"
                                    v-model="form.deliveryTime" type="date" placeholder="选择日期">
                                </el-date-picker>
                            </el-form-item>
                        </div>
                        <el-form-item label="电台名称:">
                            <div class="flex">
                                <el-input class="zt-input" v-model="form.radioStationName" placeholder="电台名称" />
                                <el-button class="bt-blue " @click="submitForm"
                                    style="margin-left: 50px; ">确定</el-button>
                            </div>
                        </el-form-item>

                        <el-form-item label="备注:" prop="remark">
                            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                    </el-form>

                    <!-- 用户电台 -->
                    <!-- <el-form ref="form" :model="form" :rules="rules" label-width="120px" v-else>
                        <el-form-item label="类型:">
                            <div class="flex">
                                <el-select v-model="form.radioStationType" filterable placeholder="类型"
                                    @change="changeType" class="zt-input-min rihgt-m5"
                                    style="width: 120px;font-weight: 600;">
                                    <el-option v-for="item in  dict.type.radio_station_type" :key="item.value"
                                        :label="item.label" :value="item.value" />
                                </el-select>
                                <el-form-item label="入网状态">
                                    <el-select v-model="form.isNet" filterable placeholder="入网状态" :disabled="isNet == 1"
                                        class="zt-input-min rihgt-m5">
                                        <el-option key="0" label="未入网" value="0" />
                                        <el-option key="1" label="已入网" value="1" />
                                    </el-select>
                                </el-form-item>

                            </div>
                        </el-form-item>

                        <el-form-item label="平台类型:">

                            <div class="flex">
                                <el-select v-model="form.carryType" filterable placeholder="类型"
                                    @change="changeCarryType" class="zt-input-min rihgt-m5"
                                    style="width: 120px;font-weight: 600;">
                                    <el-option v-for="item in  dict.type.radio_station_loader" :key="item.value"
                                        :label="item.label" :value="item.value" />
                                </el-select>
                            </div>

                        </el-form-item>


                        <el-form-item label="硬件版本号:">
                            <div class="flex">
                                <el-input class="zt-input-min" style="width: 120px;" v-model="form.hardwareVersion"
                                    placeholder="版本号" />
                                <el-form-item label="软件版本号:">
                                    <el-input class="zt-input-min" style="width: 120px;" v-model="form.softwareVersion"
                                        placeholder="版本号" />
                                </el-form-item>



                            </div>

                        </el-form-item>

                        <el-form-item label="电台名称:">
                            <div class="flex">
                                <el-input class="zt-input" v-model="form.radioStationName" placeholder="电台名称" />
                                <el-button class="bt-blue " @click="submitForm"
                                    style="margin-left: 50px; ">确定</el-button>
                            </div>
                        </el-form-item>
                        <el-form-item label="备注:" prop="remark">
                            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                    </el-form> -->

                </div>
            </template>
        </right-dialog>



        <right-dialog :title="title" :visible="openDetails" @closeDialog="cancel">
            <template v-slot:default>
                <div class="zt-dialog-solt-2" v-if="openDetails">
                    <div class="flex station-head-box">
                        <div class="station-head1">重庆万洲对周螺旋F006</div>
                        <div class="station-head2"><img src="@/assets/images/antenna-icon.png" /></div>
                        <div class="station-head3">
                            <div class="sh-box flex-s">
                                <div class="sh-1">旋转周螺旋F006旋转周螺旋F006</div>
                                <div class="sh-2">|</div>
                                <div class="sh-3">发射中</div>
                                <img class="sh-4" src="@/assets/images/js.png" />
                            </div>
                        </div>
                        <div class="station-head4 flex-s">
                            <!-- <img src="@/assets/images/amend.png">
                            <img src="@/assets/images/delete-bin2.png"> -->
                            <img src="@/assets/images/qqq.png" @click="toTransfer()" title="传输" />
                            <!-- <img src="@/assets/images/st.png"> -->
                        </div>
                    </div>

                    <div class="bg-black">状态信息</div>

                    <div class="flex station-information">
                        <div class="information-left">
                            <div class="flex information-ul">
                                <div class="information-li"><img src="@/assets/images/station2.png" /><span
                                        class="li-text">温度:{{ form.attribute.temperature || '' }}℃</span></div>
                                <div class="information-li"><img src="@/assets/images/station3.png" />
                                    <span class="li-text">预选波段:{{ form.attribute.waveBand }}</span>
                                </div>
                            </div>
                            <div class="flex information-ul">
                                <div class="information-li "><img src="@/assets/images/station2.png" /><span
                                        class="li-text">电流:{{ form.attribute.current || '' }}A</span></div>
                                <div class="information-li"><img src="@/assets/images/station6.png" /><span
                                        class="li-text">前向电压:{{ form.attribute.forewordValtage }}V</span></div>
                            </div>
                            <div class="flex information-ul">
                                <div class="information-li"><img src="@/assets/images/station2.png" /><span
                                        class="li-text">衰减:{{ form.attribute.gain }}dB</span></div>
                                <div class="information-li"><img src="@/assets/images/station6.png" /><span
                                        class="li-text">后向电压:{{ form.attribute.backValtage }}V</span></div>
                            </div>
                        </div>
                        <div class="information-right">
                        </div>

                    </div>

                    <!-- <div class="bg-blue flex-s">
                        <div>当前执行任务:重庆万州电台连续测试</div>
                    </div>

                    <div class="bg-black">发送信息</div>

                    <div class="send-box">
                        <div class="send-1"></div>
                        <div class="send-2"></div>
                    </div>

                    <div class="bg-black">频率图</div> -->
                </div>
            </template>
        </right-dialog>


    </div>
</template>

<script>
import { listStation, getStation, delStation, addStation, updateStation, restartRadioStation } from "@/api/business/station";
import { getAntennaListAll } from "@/api/business/antenna";
import RightDialog from "@/components/TZ/RightDialog"

export default {
    components: { RightDialog },
    dicts: ['radio_station_type', 'located_address', 'radio_station_loader'],
    name: "Station",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 电台表格数据
            stationList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            openDetails: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                radioStationName: null,
                radioStationType: null,
                carryType: null,
                radioStationCode: null,
                unit: null,
                address: null,
                antennaId: null,
                softwareVersion: null,
                hardwareVersion: null,
                radioStationGps: null,
                isNet: null,
                status: null,
            },
            //天线集合
            antennaListAll: [],
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            },
            isNet: 0,
            options: [{
                value: '1',
                label: '1'
            }, {
                value: '天线',
                label: '天线'
            }, {
                value: '2',
                label: '2'
            }, {
                value: '3',
                label: '3'
            }, {
                value: '天下七七',
                label: '天下七七'
            }],
            typeListAll: false,//type:true=新增,false=修改 
            antennaListAll: [//天线集合
                { label: '已使用', options: [] },
                { label: '未使用', options: [] },
            ],
            popoverOpen: false,
        };
    },
    created() {
        this.getList();
    },

    methods: {
        // restartOpen() {},
        // restartClose() {},
        postRestartRadioStation(row) {
            const data = {
                "cmd": "radio-restart",
                "data": {
                    "radioStationCode": row.radioStationCode
                }
            }
            restartRadioStation(data).then(response => {
                this.$modal.msgSuccess("重启成功");
            });
        },
        async getListAll(index) {//查询数据集合 
            this.resetListAll();
            const data = {
                locatedAddress: index,
                isUsed: 0,
            }
            await getAntennaListAll(data).then(response => {
                this.antennaListAll[1].options = response.rows;
            });
            console.log(this.antennaListAll[1].options);
        },
        resetListAll() {
            this.antennaListAll = [//天线集合
                { label: '已使用', options: [] },
                { label: '未使用', options: [] },
            ]
        },
        /** 查询电台列表 */
        getList() {
            this.loading = true;
            listStation(this.queryParams).then(response => {
                this.stationList = response.rows;
                this.total = response.total;
                this.loading = false;
                console.log("电台列表：" + this.total);
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.openDetails = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                radioStationMode: null,
                radioStationId: null,
                radioStationName: null,
                radioStationType: null,
                carryType: null,
                radioStationCode: null,
                locatedAddress: null,
                unit: null,
                address: null,
                antennaId: null,
                softwareVersion: null,
                hardwareVersion: null,
                radioStationGps: null,
                isUsed: null,
                isNet: null,
                status: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null,
                pltAntennaList: null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.radioStationId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加电台";
            this.typeListAll = true;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            //this.reset();
            this.typeListAll = false;
            const radioStationId = row.radioStationId || this.ids
            getStation(radioStationId).then(response => {
                this.form = null;
                this.form = response.data;
                this.open = true;
                this.isNet = this.form.isNet;
                this.title = "修改电台";

                if (this.form.locatedAddress) {
                    this.getListAll(this.form.locatedAddress);
                    this.getAntennaUsed();
                    this.transitionList();
                }

            });
        },
        getAntennaUsed() {
            this.antennaListAll[0].options = [];
            for (let i = 0; i < this.form.pltAntennaList.length; i++) {
                if (this.form.pltAntennaList[i].isUsed * 1 == 1) {
                    const data = {
                        antennaId: this.form.pltAntennaList[i].antennaId,
                        isUsed: 0,
                        antennaName: this.form.pltAntennaList[i].antennaName,
                    }
                    this.antennaListAll[0].options.push(data)
                    console.log(this.antennaListAll[0]);
                    this.$set(this.form, 'antennaId', this.form.pltAntennaList[i].antennaId);
                }
            }

        },
        transitionList() {
            // this.$set(this.form, 'antennaIds', this.beforeForm.antennaList ? this.beforeForm.sensorList.map(item => item.sensorId) : []);
            // this.$set(this.form, 'cameraIds', this.beforeForm.cameraList ? this.beforeForm.cameraList.map(item => item.cameraId) : []);
            // this.$set(this.form, 'radioStationIds', this.beforeForm.radioStationList ? this.beforeForm.radioStationList.map(item => item.radioStationId) : []);
        },
        handleDetails(row) {
            //this.reset();
            const radioStationId = row.radioStationId || this.ids
            getStation(radioStationId).then(response => {
                if (response.data.attribute) {
                    response.data.attribute = JSON.parse(response.data.attribute)
                }
                this.form = response.data;
                this.openDetails = true;
                this.title = "电台详情";
            });
        },
        //提交前将传感器转换
        setPltAntennaList() {
            let data = []
            if (this.antennaListAll[0].options.length >= 1) data.push(this.antennaListAll[0].options[0])
            if (this.form.antennaId) data.push({ antennaId: this.form.antennaId, isUsed: 1 })
            data = data.filter(item => item.antennaId != null && item.antennaId != []);
            this.$set(this.form, 'pltAntennaList', data);
        },
        deleteEmptyAntennaId() {
            // Check if antennaId is an empty array, then delete the data property
            if (this.data.antennaId.length === 0) {
                this.$delete(this, 'data');
            }
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.setPltAntennaList();
                    if (this.form.radioStationId != null) {
                        updateStation(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addStation(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });

        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const radioStationIds = row.radioStationId || this.ids;
            this.$modal.confirm('是否确认删除电台编号为"' + radioStationIds + '"的数据项？').then(function () {
                return delStation(radioStationIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/station/export', {
                ...this.queryParams
            }, `station_${new Date().getTime()}.xlsx`)
        },
        getLocatedAddress(index) {
            const entries = this.dict.type.located_address;
            for (let i = 0; i < entries.length; i++) {
                if (entries[i].value == index) {
                    return entries[i].label
                }
            }
            return ""
        },
        getRadioStationType(index) {
            const entries = this.dict.type.radio_station_type;
            for (let i = 0; i < entries.length; i++) {
                if (entries[i].value == index) {
                    return entries[i].label
                }
            }
            return ""
        },

        changeType(index) {
            const id = this.form.radioStationId
            this.form.radioStationName = this.getLocatedAddress(this.form.locatedAddress) + this.getRadioStationType(index);
            // this.reset();
            this.form.radioStationId = id
            this.form.radioStationType = index
            this.form.isNet = this.isNet

        },
        changeLocatedAddress(index) {
            // this.form.antennaId = null
            this.$set(this.form, 'antennaId', null);
            this.getListAll(index);
        },
        changeCarryType(index) {
            this.form.unit = null
            this.form.locatedAddress = null
        },
        /** 跳转到传输页面 */
        toTransfer() {
            this.$router.push({ path: '/radio/transfer/index', query: { srcId: this.form.radioStationCode } });
        }
    }
};
</script>


<style rel="stylesheet/scss" lang="scss" scoped></style>